package com.fankui.serviceucenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fankui.commonutils.JwtUtils;
import com.fankui.commonutils.MD5;
import com.fankui.servicebase.exceptionhandler.LegendaryException;
import com.fankui.serviceucenter.entity.RegisterVo;
import com.fankui.serviceucenter.entity.UcenterMember;
import com.fankui.serviceucenter.mapper.UcenterMemberMapper;
import com.fankui.serviceucenter.service.UcenterMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author 范奎
 * @since 2023-12-06
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {
    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Override
    public String login(UcenterMember member) {
        String mobile = member.getMobile();
        String password = member.getPassword();
        // 判断账号和密码是否为空
        if (StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)) {
            throw new LegendaryException(20001, "登录失败，用户名和密码不能为空！");
        }
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile", mobile);
        UcenterMember ucenterMember = baseMapper.selectOne(wrapper);
        // 判断用户是否注册
        if (ucenterMember == null) {
            throw new LegendaryException(20001, "登录失败，该用户未注册！");
        }
        // 判断密码是否正确
        if (!MD5.encrypt(password).equals(ucenterMember.getPassword())) {
            throw new LegendaryException(20001, "登录失败，密码错误！");
        }
        // 判断用户是否被禁用
        if (ucenterMember.getIsDisabled()) {
            throw new LegendaryException(20001, "登录失败，该用户已禁用！");
        }
        // 登录成功，返回token
        return JwtUtils.getJwtToken(ucenterMember.getId(), ucenterMember.getNickname());
    }

    @Override
    public boolean register(RegisterVo registerVo) {
        String mobile = registerVo.getMobile();
        String nickname = registerVo.getNickname();
        String password = registerVo.getPassword();
        String code = registerVo.getCode();
        // 判断用户名、密码、用户昵称和验证码是否为空
        if (StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password) || StringUtils.isEmpty(nickname) || StringUtils.isEmpty(code)) {
            throw new LegendaryException(20001, "注册失败，用户名、密码、用户昵称和验证码不能为空！");
        }
        String redisCode = redisTemplate.opsForValue().get(mobile);
        // 判断验证码是否输入正确
        if (!code.equals(redisCode)) {
            throw new LegendaryException(20001, "注册失败，验证码错误！");
        }
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile", mobile);
        Integer count = baseMapper.selectCount(wrapper);
        // 判断手机号是否已经注册成功过
        if (count > 0) {
            throw new LegendaryException(20001, "注册失败，该手机号已经注册过");
        }
        UcenterMember member = new UcenterMember();
        BeanUtils.copyProperties(registerVo, member);
        member.setPassword(MD5.encrypt(password));
        member.setIsDisabled(false);
        // 注册成功
        int insert = baseMapper.insert(member);
        if (insert > 0) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public UcenterMember getOpenIdMember(String openid) {
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("openid",openid);
        UcenterMember member = baseMapper.selectOne(wrapper);
        return member;
    }

    @Override
    public Integer getRegisterCountByDay(String day) {
        return baseMapper.getRegisterCountByDay(day);
    }
}
